package model

import (
	"errors"
	"log"
	"raymond/global"
	"time"

	"github.com/jmoiron/sqlx"
)

const table_tron_transaction_journal = "t_tron_transaction_journal"

type TronTransactionJournal struct {
	Id              int64     `db:"id"`
	BlockNumber     string    `db:"block_number"`
	TimeStamp       string    `db:"time_stamp"`
	Hash            string    `db:"hash"`
	BlockHash       string    `db:"block_hash"`
	FromAddr        string    `db:"from_addr"`
	ContractAddress string    `db:"contract_address"`
	ToAddr          string    `db:"to_addr"`
	Value           string    `db:"value"`
	InputData       string    `db:"input_data"`
	Confirmations   string    `db:"confirmations"`
	Remark          string    `db:"remark"`
	CreateTime      time.Time `db:"ct"`
	UpdateTime      time.Time `db:"ut"`
}

func (tx *TronTransactionJournal) CreateTronTxJournal() {
	now := time.Now().UTC()
	model := &TronTransactionJournal{
		BlockNumber:     tx.BlockNumber,
		TimeStamp:       tx.TimeStamp,
		Hash:            tx.Hash,
		BlockHash:       tx.BlockHash,
		FromAddr:        tx.FromAddr,
		ContractAddress: tx.ContractAddress,
		ToAddr:          tx.ToAddr,
		Value:           tx.Value,
		InputData:       tx.InputData,
		Confirmations:   tx.Confirmations,
		Remark:          tx.Remark,
		CreateTime:      now,
		UpdateTime:      now,
	}
	result, err := global.DB().NamedExec("insert into "+table_tron_transaction_journal+
		" (block_number, time_stamp, hash, block_hash, from_addr, contract_address, to_addr, value, "+
		" input_data, confirmations, "+
		" create_time, update_time)"+
		" values (:block_number, :time_stamp, :hash, :block_hash, :from_addr, :contract_address, :to_addr, :value, "+
		" :input_data, :confirmations, "+
		" :ct,:ut)",
		&model)
	if err != nil {
		log.Println(err)
		return
	}
	tx.Id, _ = result.LastInsertId()
}

func AllTronTxList() *[]TronTransactionJournal {
	var txList []TronTransactionJournal
	err := global.DB().Select(&txList, "select hash, from_addr, to_addr from "+table_tron_transaction_journal)
	if err != nil {
		log.Println(err)
		return nil
	}
	return &txList
}

func BatchInsertTronTxJournal(txJournals *[]TronTransactionJournal) error {
	if txJournals == nil || len(*txJournals) <= 0 {
		return errors.New("空数组不能批量插入")
	}
	now := time.Now().UTC()
	insert := "insert into " + table_tron_transaction_journal +
		" (block_number, time_stamp, hash, block_hash, " +
		" from_addr, contract_address, to_addr, value, " +
		" input_data, confirmations, " +
		" create_time, update_time) values "

	vals := []interface{}{}
	for _, tj := range *txJournals {
		insert += "(?,?,?,?, ?,?,?,?, ?,?, ?,?),"
		vals = append(vals, tj.BlockNumber, tj.TimeStamp, tj.Hash, tj.BlockHash,
			tj.FromAddr, tj.ContractAddress, tj.ToAddr, tj.Value,
			tj.InputData, tj.Confirmations,
			now, now)
	}
	// 去掉最后的逗号
	insert = insert[0 : len(insert)-1]

	insert, args, err := sqlx.In(insert, vals...)
	if err != nil {
		return err
	}

	insert = global.DB().Rebind(insert)

	_, err = global.DB().Exec(insert, args...)
	if err != nil {
		return err
	}
	return nil
}
